/*
 * Copyright (c) 2010. raoqn
 */

package com.yfeng.goodjuhome.action;

import com.yfeng.exception.ActionException;
import com.yfeng.goodjuhome.model.MailModel;
import com.yfeng.goodjuhome.model.MessageModel;
import com.yfeng.goodjuhome.model.User;
import com.yfeng.goodjuhome.service.IHomeMailService;
import com.yfeng.goodjuhome.service.IMessageService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * 站内邮件
 * www.goodjuhome.com
 * Created by IntelliJ IDEA.
 * User: rqn
 * Date: 2010-8-14
 * Time: 21:42:45
 */
@Controller("homeMailAction")
public class HomeMailAction {
     private final Log log = LogFactory.getLog(this.getClass());
    
    @Autowired
    @Qualifier("homeMailService")
    private IHomeMailService homeMailService;

    //
    @RequestMapping("/home/homemail/toSendeMailByAjax")
    public void toSendMailByAjax(HttpServletRequest request, HttpServletResponse response, HttpSession session,String validWord)  throws ActionException
    {
        try {

			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();

            if(session.getAttribute("currUser")==null)
                write.print("<script>alert('请先登录!'); history.back();</script>");
            
			String rand=session.getAttribute("rand").toString();
			if(!validWord.equals(rand)){
				write.print("<script>alert('验证码错误!'); history.back();</script>");
			}

            String uid = ((User)session.getAttribute("currUser")).getId();
            String ruid = request.getParameter("ruid"); //获取接收留言的人的ID，从页面传过来
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            MailModel model = new MailModel();
            model.setSend_uid(uid);
            model.setAccept_uid(ruid);
            model.setTitle(title);
            model.setContent(content);


			int ret = homeMailService.sendMail(model);
			if(ret>0){
				write.print("<script>alert('邮件发送成功!');</script>");
			}else {
				write.print("<script>alert('邮件发送失败!');history.back();</script>");
			}

		} catch (Exception e) {
			log.error(e.getMessage(),e);
            throw new ActionException(e.getMessage(),e);
		}
    }


     @RequestMapping("/home/homemail/toSendMail")
    public void toSendMail(HttpServletRequest request, HttpServletResponse response, HttpSession session,String validWord)  throws ActionException
    {
        try {

			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();

            if(session.getAttribute("currUser")==null)
                write.print("<script>alert('请先登录!'); history.back();</script>");

			String rand=session.getAttribute("rand").toString();
			if(!validWord.equals(rand)){
				write.print("<script>alert('验证码错误!'); history.back();</script>");
			}

            String uid = ((User)session.getAttribute("currUser")).getId();
            String ruid = request.getParameter("ruid"); //获取接收留言的人的ID，从页面传过来
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            MailModel model = new MailModel();
            model.setSend_uid(uid);
            model.setAccept_uid(ruid);
            model.setTitle(title);
            model.setContent(content);


			int ret = homeMailService.sendMail(model);
			if(ret>0){
				write.print("<script>alert('邮件发送成功!');window.location='"+ request.getContextPath()+ "/home/e/mail/index';</script>");
			}else {
				write.print("<script>alert('邮件发送失败!');history.back();</script>");
			}

		} catch (Exception e) {
			log.error(e.getMessage(),e);
            throw new ActionException(e.getMessage(),e);
		}
    }

     @RequestMapping("/home/homemail/toDeleteMail")
    public void toDeleteMail(HttpServletRequest request, HttpServletResponse response, HttpSession session)  throws ActionException
    {
        try {

			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();

            if(session.getAttribute("currUser")==null)
                write.print("<script>alert('请先登录!'); history.back();</script>");


            String[] ids  =request.getParameterValues("id");

			int ret = homeMailService.deleteMail(ids);
			if(ret>0){
				write.print("<script>alert('邮件删除成功!');window.location='"+request.getContextPath()+"/home/e/mail/index';</script>");
			}else {
				write.print("<script>alert('邮件删除失败!');history.back();</script>");
			}

		} catch (Exception e) {
			log.error(e.getMessage(),e);
            throw new ActionException(e.getMessage(),e);
		}
    }

}
